#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

import sys


def Verify_Fasta(filename, count):
    import subprocess

    cmd2 = "grep '>' " + filename + " | wc -l"
    p2 = subprocess.Popen(cmd2, stdout=subprocess.PIPE, shell=True)
    p2.wait()
    result2 = ""
    for line in p2.stdout.readlines():
        line = line.decode("utf-8").strip()
        result2 += line
    if int(result2.strip()) == count:
        cmd = "grep '<html>' " + filename
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
        p.wait()
        result = ""
        for line in p.stdout.readlines():
            line = line.decode("utf-8").rstrip()
            result += line
        if result.find("<html>") != -1:
            print("Invalid because of <html>")
            return False
        elif result.find("No such file or directory") != -1:
            print("Invalid because of no file")
            return False
        else:
            return True
    else:
        print("Invalid because not enough fasta, real fasta count:" + result2.strip())
        return False


if __name__ == '__main__':
    print(Verify_Fasta(sys.argv[1], int(sys.argv[2])))
